import logging
import unittest
import requests
from parameterized import parameterized
from api.openAccountApi import OpenAccountApi
from api.regLoginApi import RegLoginApi
from common.common_assert import common_assert, common_assert1
from common.html_util import html_util
from common.read_json import read_json


class TestOpenAccount(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        self.reg_login_api = RegLoginApi(self.session)
        self.open_account_api = OpenAccountApi(self.session)
        # 实例化封装接口请求的类

    def tearDown(self) -> None:
        self.session.close()
        # 关闭session对象

    # 实名认证测试用例
    @parameterized.expand(read_json('realname', 'real_name'))
    def test01_realname(self, case_name, url_login_body, body, exp_status_code, exp_status, description):
        # 打印测试用例名称
        print(case_name)
        logging.info(case_name)
        # 实现登录接口
        self.reg_login_api.user_login(url_login_body)

        # 实现实名认证接口
        res = self.open_account_api.realname(body=body)

        # 断言
        common_assert1(self, res, exp_status_code, exp_status, description, case_name)

    # 开户测试用例
    def test02_open_account(self):
        body = {
            "keywords": "18708309001",
            "password": "123456a"
        }

        # 实现登录接口
        self.reg_login_api.user_login(body)
        # 实现开户接口
        res = self.open_account_api.open_account()
        # 断言
        print(res.json)
        common_assert(self, res, 200, 200, 'form')

    # 第三方开户测试用例
    def test03_third_open_account(self):
        body = {
            "keywords": "18708309001",
            "password": "123456a"
        }

        # 实现登录接口
        self.reg_login_api.user_login(body)
        # 实现开户接口
        res = self.open_account_api.open_account()
        # 实现第三方开户接口
        a = html_util(response=res)
        # print(a)
        res = self.open_account_api.third_open_account(url=a[0], form_dict=a[1])

        # 断言

        self.assertEqual(200, res.status_code)
        self.assertIn('OK', res.text)
